/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.shop;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopNewVipCoupon;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopCouponService;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopNewVipCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 新人专享优惠券Controller
 *
 * @author 李欣荣
 * @version 2018-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/shop/hkShopNewVipCoupon")
public class HkShopNewVipCouponController extends BaseController {

    @Autowired
    private HkShopNewVipCouponService hkShopNewVipCouponService;

    @Autowired
    private HkShopCouponService hkShopCouponService;

    @ModelAttribute
    public HkShopNewVipCoupon get(@RequestParam(required = false) String id) {
        HkShopNewVipCoupon entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkShopNewVipCouponService.get(id);
        }
        if (entity == null) {
            entity = new HkShopNewVipCoupon();
        }
        return entity;
    }

    @RequiresPermissions("shop:shop:hkShopNewVipCoupon:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkShopNewVipCoupon hkShopNewVipCoupon, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<HkShopNewVipCoupon> page = hkShopNewVipCouponService.findPage(new Page<HkShopNewVipCoupon>(request, response), hkShopNewVipCoupon);
        model.addAttribute("page", page);
        return "hk/shop/shop/hkShopNewVipCouponList";
    }

    @RequiresPermissions("shop:shop:hkShopNewVipCoupon:view")
    @RequestMapping(value = "form")
    public String form(HkShopNewVipCoupon hkShopNewVipCoupon, Model model) {
        model.addAttribute("hkShopNewVipCoupon", hkShopNewVipCoupon);
        return "hk/shop/shop/hkShopNewVipCouponForm";
    }

    @RequiresPermissions("shop:shop:hkShopNewVipCoupon:edit")
    @RequestMapping(value = "save")
    public String save(HkShopNewVipCoupon hkShopNewVipCoupon, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, hkShopNewVipCoupon)) {
            return form(hkShopNewVipCoupon, model);
        }

        /**
         * 根据优惠券ID获取商品ID、优惠券生效时间、优惠券过期时间
         */
        String shopPouponId = hkShopNewVipCoupon.getCoupon().getId();
        if (StringUtils.isBlank(shopPouponId)) {
            addMessage(redirectAttributes, "保存新人专享优惠券失败");
            return "redirect:" + Global.getAdminPath() + "/shop/shop/hkShopNewVipCoupon/?repage";
        } else {
            HkShopCoupon hkShopCoupon = hkShopCouponService.get(shopPouponId);
            hkShopNewVipCoupon.setProductId(hkShopCoupon.getProduct().getId());    //商品ID
            hkShopNewVipCoupon.setDiscount(hkShopCoupon.getDiscount());  //折扣
            hkShopNewVipCouponService.save(hkShopNewVipCoupon);
            addMessage(redirectAttributes, "保存新人专享优惠券成功");
            return "redirect:" + Global.getAdminPath() + "/shop/shop/hkShopNewVipCoupon/?repage";
        }
    }

    @RequiresPermissions("shop:shop:hkShopNewVipCoupon:edit")
    @RequestMapping(value = "delete")
    public String delete(HkShopNewVipCoupon hkShopNewVipCoupon, RedirectAttributes redirectAttributes) {
        hkShopNewVipCouponService.delete(hkShopNewVipCoupon);
        addMessage(redirectAttributes, "删除新人专享优惠券成功");
        return "redirect:" + Global.getAdminPath() + "/shop/shop/hkShopNewVipCoupon/?repage";
    }

}